package com.syxbbs.forum.controller;


import com.syxbbs.forum.common.AppResult;
import com.syxbbs.forum.common.ResultCode;
import com.syxbbs.forum.config.AppConfig;
import com.syxbbs.forum.exception.ApplicationException;
import com.syxbbs.forum.model.Article;
import com.syxbbs.forum.model.Board;
import com.syxbbs.forum.model.User;
import com.syxbbs.forum.services.IArticleService;
import com.syxbbs.forum.services.IBoardServive;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@Slf4j
@RequestMapping(value = "/article", produces = "application/json")
public class ArticleController {
    @Resource
    IArticleService iArticleService;

    @Resource
    IBoardServive iBoardServive;

    @RequestMapping("/create")
    //发布新帖子
    public AppResult create(HttpServletRequest request, @RequestParam("boardId") @NonNull Long boardId, @RequestParam("title") @NonNull String title, @RequestParam("content") @NonNull String content){
        //校验用户禁言状态
        HttpSession session = request.getSession(false);
        User user= (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);

        }
        //板块校验
        Board board=iBoardServive.selectById(boardId);
        if (board==null || board.getDeleteState()==1 || board.getState()==1){
            log.warn(ResultCode.FAILED_BOARD_BANNED.toString());
            throw new ApplicationException(ResultCode.FAILED_BOARD_BANNED.toString());
        }
        //封装文章对象
        Article article=new Article();
        article.setTitle(title);//标题
        article.setContent(content);
        article.setUserId(user.getId());
        article.setBoardId(boardId);
        iArticleService.create(article);

        return AppResult.success();
    }


    @RequestMapping("/getAllByBoardId")
    //非必传
    public AppResult<List<Article>> getAllByBoardId(@RequestParam(value = "boardId",required = false)    Long boardId){
        List<Article> articles;
        if (boardId==null) {
            articles = iArticleService.selectAll();

        }
        else {
            articles=iArticleService.selectAllByBoardId(boardId);
        }
        if (articles == null) {
            //创建为空，返回数据为空字符串
            articles = new ArrayList<>();
        }
        return AppResult.success(articles);
    }




    @RequestMapping("/details")
    public AppResult getDetails( HttpServletRequest request,@RequestParam("id") Long id){
        HttpSession session = request.getSession(false);
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        Article article = iArticleService.selectDetailById(id);
        if (article==null){
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);

        }
        if (article.getUserId()==user.getId()){
            article.setOwn(true);
        }else {
            article.setOwn(false);
        }
        return AppResult.success(article);


    }


    @RequestMapping("/modify")//看看用户修改文章
    public AppResult modify(HttpServletRequest request,@NonNull Long id,@NonNull String title,@NonNull String content){
        HttpSession session = request.getSession();
        User user= (User)session.getAttribute(AppConfig.USER_SESSION);
        //检验用户登录 状态
        if (user.getState()==1){
            //用户被禁言了
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }

        Article article=iArticleService.selectById(id);
        if (article==null){
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (user.getId()!=article.getUserId()){
            return AppResult.failure(ResultCode.FAILED);
        }
        if (article.getState()==1 || article.getDeleteState()==1){
            return AppResult.failure(ResultCode.FAILED_ARTICLE_BANNED);
        }
        iArticleService.modify(id,title,content);
        log.info("帖子更新成功");
        return AppResult.success();


    }

    @RequestMapping("/thumbUp")
    public AppResult thumbUp(HttpServletRequest request,@RequestParam("id") @NonNull Long id){
        //看用户是否被禁言
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState()==1){
            return AppResult.failure(ResultCode.FAILED_USER_BANNED);
        }
        //调用service
        iArticleService.thumbUpById(id);
        return AppResult.success();

    }


    @RequestMapping("/delete")
    public AppResult deleteById(HttpServletRequest request,@RequestParam("id") Long id){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(AppConfig.USER_SESSION);
        Article article = iArticleService.selectById(id);
        if (article.getDeleteState()==1 || article==null){
            return AppResult.failure(ResultCode.FAILED_ARTICLE_NOT_EXISTS);
        }
        if (user.getId()!=article.getUserId()){
            return AppResult.failure(ResultCode.FAILED_FORBIDDEN);
        }
        //调用service
        iArticleService.deleteById(id);
        return AppResult.success();


    }

    //个人主页信息，，发帖信息
    @RequestMapping("/getAllByUserId")
    public AppResult getAllByUserId(HttpServletRequest request,@RequestParam(value = "userId",required = false) Long userId){
        if (userId==null){
            HttpSession session = request.getSession(false);
            User user = (User) session.getAttribute(AppConfig.USER_SESSION);
            userId= user.getId();

        }
        List<Article> articles = iArticleService.selectByUserId(userId);
        return AppResult.success(articles);
    }

}
